package com.ruoyi.crm.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.CustomerInfo;
import com.ruoyi.crm.domain.LeadInfo;
import com.ruoyi.crm.mapper.LeadMapper;
import com.ruoyi.crm.service.ILeaderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class LeaderServiceImpl implements ILeaderService {
    private final LeadMapper leadMapper;

    /**
     * 查询客户线索
     */
    @Override
    public TableDataInfo<LeadInfo> selectLeadPageList(LeadInfo leadInfo, PageQuery pageQuery) {
        Map<String, Object> params = leadInfo.getParams();
        LambdaQueryWrapper<LeadInfo> query = new LambdaQueryWrapper<>();

        if (leadInfo.getCustomerId()!=null && leadInfo.getCustomerId()>0 ) {
            query.inSql(LeadInfo::getCustomerId,
                "select customer_id from crm_customer_info where customer_name like '%" + leadInfo.getCustomerId() + "%'");
        }

        if (leadInfo.getCompanySizeId()!=null && leadInfo.getCompanySizeId()>0 ) {
            query.inSql(LeadInfo::getCompanySizeId,
                "select company_size_id from crm_company_size where license_number like '%" + leadInfo.getCompanySizeId() + "%'");
        }

        if (leadInfo.getResponsiblePersonId()!=null && leadInfo.getResponsiblePersonId()>0 ) {
            query.inSql(LeadInfo::getResponsiblePersonId,
                "select responsible_person_id from crm_responsible_info where responsible_person_name like '%" + leadInfo.getResponsiblePersonId() + "%'");
        }

        if (leadInfo.getCustomerTypeId()!=null && leadInfo.getCustomerTypeId()>0) {
            query.inSql(LeadInfo::getCustomerTypeId,
                "select customer_type_id from crm_customer_type where type like '%" + leadInfo.getCustomerTypeId() + "%'");
        }

        if (params != null && params.get("beginTime") != null && params.get("endTime") != null) {
            query.between(LeadInfo::getNextTime, params.get("beginTime"), params.get("endTime"));
        }

        query.orderByAsc(LeadInfo::getNextTime);
        Page<LeadInfo> page = leadMapper.selectPage(pageQuery.build(), query);
        return TableDataInfo.build(page);
    }
    /**
     * 根据客户ID获取详细信息
     *
     * @param leadId 客户ID
     */
    @Override
    public LeadInfo selectLeadById(Long leadId) {
        return leadMapper.selectById(leadId);
    }

    /**
     * 新增客户线索
     * @param leadInfo
     * @return
     */
    @Override
    public int insertLeadById(LeadInfo leadInfo) {
        return leadMapper.insert(leadInfo);
    }

    /**
     * 修改客户线索
     * @param leadInfo
     * @return
     */
    @Override
    public int updateLeadById(LeadInfo leadInfo) {
        return leadMapper.updateById(leadInfo);
    }

    /**
     * 删除客户线索
     * @param leadId
     * @return
     */
    @Override
    public int deleteLeadById(Long leadId) {
        return leadMapper.deleteById(leadId);
    }

    /**
     * 导出客户线索
     * @param leadInfo
     * @return
     */

    @Override
    public List<LeadInfo> selectLeadList(LeadInfo leadInfo) {
        Map<String, Object> params = leadInfo.getParams();
        LambdaQueryWrapper<LeadInfo> query = new LambdaQueryWrapper<>();

        if (leadInfo.getCustomerId()!=null && leadInfo.getCustomerId()>0 ) {
            query.inSql(LeadInfo::getCustomerId,
                "select customer_id from customer_info where customer_name like '%" + leadInfo.getCustomerId() + "%'");
        }

        if (leadInfo.getCompanySizeId()!=null && leadInfo.getCompanySizeId()>0 ) {
            query.inSql(LeadInfo::getCompanySizeId,
                "select company_size_id from company_size where license_number like '%" + leadInfo.getCompanySizeId() + "%'");
        }

        if (leadInfo.getResponsiblePersonId()!=null && leadInfo.getResponsiblePersonId()>0 ) {
            query.inSql(LeadInfo::getResponsiblePersonId,
                "select responsible_person_id from responsible_info where responsible_person_name like '%" + leadInfo.getResponsiblePersonId() + "%'");
        }

        if (leadInfo.getCustomerTypeId()!=null && leadInfo.getCustomerTypeId()>0) {
            query.inSql(LeadInfo::getCustomerTypeId,
                "select customer_type_id from customer_type where type like '%" + leadInfo.getCustomerTypeId() + "%'");
        }

        if (params != null && params.get("beginTime") != null && params.get("endTime") != null) {
            query.between(LeadInfo::getNextTime, params.get("beginTime"), params.get("endTime"));
        }

        query.orderByAsc(LeadInfo::getNextTime);

        return leadMapper.selectList(query);
    }

    @Override
    public boolean insertBatch(List<LeadInfo> list) {
        return leadMapper.insertBatch(list);
    }

}
